<?php
function get_tag_data_counter($short_name, $sms, $web, $multiple, &$entry_date, &$entry_from, &$entry_answer, &$entry_times){
	$authhost="{imap.gmail.com:993/imap/ssl/novalidate-cert}";
	$user="lm.quanda@gmail.com";
	$pass="lockheed+martin";
	$imap = imap_open( $authhost, $user, $pass );
	$num = imap_num_msg($imap);
	$messages = imap_sort($imap, SORTDATE, 0);
	$i=0;
	foreach($messages as $message){
			$header = imap_header($imap, $message);
			$date = date("Y-m-d H:i:00", $header->udate);
			$who = $header->fromaddress;
			$complete_body = (imap_fetchbody($imap, $header->Msgno, 1));
			$from = explode(' <',$who);
			$body = explode(' ',$complete_body);
			$answer = explode('--',$body[1]);
			if(trim(strtolower($body[0]))==trim(strtolower($_GET['short_name']))){
				$entry_date_temp[]=$date;
				$entry_from_temp[]=trim($from[0]);
				$entry_answer_temp[]=trim($answer[0]);
			}
	}
	if($multiple=='No'){
		delete_multiple_entries($entry_date_temp, $entry_from_temp, $entry_answer_temp);
	}
	if($sms=='No'){
		delete_sms_entries($entry_date_temp, $entry_from_temp, $entry_answer_temp);
	}
	if($web=='No'){
		delete_web_entries($entry_date_temp, $entry_from_temp, $entry_answer_temp);
	}	
	$counter=count($entry_answer_temp);
	$w=0;
	foreach($entry_answer_temp as $val){
		$i=0;
		$x=0;
		while($i<$counter){
			if($entry_answer_temp[$i]==$val){
				$x++;
			}
			$i++;
		}
		//delete_multiple_entries($entry_date_temp, $entry_from_temp, $entry_answer_temp);
		$entry_date[$w]=$entry_date_temp[$w];
		$entry_from[$w]=$entry_from_temp[$w];
		$entry_answer[$w]=$entry_answer_temp[$w];
		$entry_times[$w]=$x;
		$w++;
	}
	//echo'<pre>';print_r($tag);echo'</pre>';die();
	imap_close($imap);
}
function delete_multiple_entries(&$entry_date, &$entry_from, &$entry_answer){
	$counter=count($entry_date);
	$answer=array();
	$date=array();
	$from=array();
	$w=0;
	$x=0;
	for($i=0; $i<$counter; $i++){	
			if(!in_array($entry_from[$i],$from)){
				$answer[$x]=$entry_answer[$i];
				$date[$x]=$entry_date[$i];
				$from[$x]=$entry_from[$i];	
				$x++;					
			}
	}
	$entry_answer=$answer;
	$entry_date=$date;
	$entry_from=$from;
}
function delete_web_entries(&$entry_date, &$entry_from, &$entry_answer){
	$counter=count($entry_date);
	$answer=array();
	$date=array();
	$from=array();
	$k=0;
	for($i=0; $i<$counter; $i++){
		if(!validate_email_yesSMS($entry_from[$i]) && !only_char($entry_from[$i])){
			$answer[$k]=$entry_answer[$i];
			$date[$k]=$entry_date[$i];
			$from[$k]=$entry_from[$i];
			$k++;
		}
	}
	$entry_answer=$answer;
	$entry_date=$date;
	$entry_from=$from;
}
function delete_sms_entries(&$entry_date, &$entry_from, &$entry_answer){
	$counter=count($entry_date);
	$answer=array();
	$date=array();
	$from=array();
	$k=0;
	for($i=0; $i<$counter; $i++){
		$c=$entry_from[$i];
		$counter2=strlen($c);
		$x=0;		
		for($w=0; $w<$counter2; $w++){			
			if ($c[$w] >= '0' && $c[$w] <= '9'){
				$x++;
			}
		}		
		if($x<10){
			$answer[$k]=$entry_answer[$i];
			$date[$k]=$entry_date[$i];
			$from[$k]=$entry_from[$i];
			$k++;	
		}
	}
	$entry_answer=$answer;
	$entry_date=$date;
	$entry_from=$from;
}
function get_tag_data($short_name, &$tag){
	$authhost="{imap.gmail.com:993/imap/ssl/novalidate-cert}";
	$user="lm.quanda@gmail.com";
	$pass="lockheed+martin";
	$imap = imap_open( $authhost, $user, $pass );
	$num = imap_num_msg($imap);
	$messages = imap_sort($imap, SORTDATE, 0);
	$i=0;
	foreach($messages as $message){
			$header = imap_header($imap, $message);
			$complete_body = (imap_fetchbody($imap, $header->Msgno, 1));
			$body = explode(' ',$complete_body);
			$answer = explode('--',$body[1]);
			if(trim(strtolower($body[0]))==trim(strtolower($short_name))){
				$entry_answer[]=trim($answer[0]);
			}
	}
	$counter=count($entry_answer);
	$tag=array();
	foreach($entry_answer as $val){
		$i=0;
		$x=0;
		while($i<$counter){
			if($entry_answer[$i]==$val){
				$x++;
			}
			$i++;
		}
		$tag[$val]=$x;
	}
	//echo'<pre>';print_r($tag);echo'</pre>';die();
	imap_close($imap);
}
function get_tag_cloud() {
	$min_font_size = 12;
	$max_font_size = 100;
	$tag=array();
	get_tag_data($_GET['short_name'], $tag);
	$cloud_html = '';
	$cloud_tags = array();
	$i=0;
	foreach ($tag as $tag => $count) {
		$size = $min_font_size + ($count*10);
		$cloud_tags[] = '<span id="'.$i.'" style="font-size: '.floor($size).'px; cursor:move;'.'"class="tag_cloud" 
		title="\''.$tag.'\' returned a count of '.$count.'">'.htmlspecialchars(stripslashes($tag)).'</span>'.
		'<script type="text/javascript">new Draggable(\''.$i.'\');</script>';
		$i++;
	}
	return $cloud_tags;
}
function get_poll_data($short_name, &$entry_date, &$entry_from, &$entry_answer){
	$authhost="{imap.gmail.com:993/imap/ssl/novalidate-cert}";
	$user="lm.quanda@gmail.com";
	$pass="lockheed+martin";
	$imap = imap_open( $authhost, $user, $pass );
	$num = imap_num_msg($imap);
	$messages = imap_sort($imap, SORTDATE, 0);
	$i=0;
	foreach ($messages as $message) {
			$header = imap_header($imap, $message);
			$date = date("Y-m-d H:i:00", $header->udate);
			$who = $header->fromaddress;
			$complete_body = (imap_fetchbody($imap, $header->Msgno, 1));
			$from = explode(' <',$who);
			$body = explode(' ',$complete_body);
			$answer = explode('--',$body[1]);
			if(trim(strtolower($body[0]))==trim(strtolower($_GET['short_name']))){
				$entry_date[]=$date;
				$entry_from[]=trim($from[0]);
				$entry_answer[]=trim($answer[0]);
			}
	}
	//echo'<pre>';print_r($entry_from);echo'</pre>';die();
	imap_close($imap);
}
function validate_email($email){
	if (substr_count($email, '@') != 1)
    	return false;
	if ($email{0} == '@')
		return false;
	if (substr_count($email, '.') < 1)
		return false;
	if (strpos($email, '..') !== false)
		return false;
	$length = strlen($email);
	for ($i = 0; $i < $length; $i++) {
		$c = $email{$i};
		if ($c >= 'A' && $c <= 'Z')
			continue;
		if ($c >= 'a' && $c <= 'z')
			continue;
		if ($c >= '0' && $c <= '9')
			continue;
		if ($c == '@' || $c == '.' || $c == '_' || $c == '-')
			continue;
		return false;
	}
	$TLD = array (
		 'COM',   'NET',
		 'ORG',   'MIL',
		 'EDU',   'GOV',
		 'BIZ',   'NAME',
		 'MOBI',  'INFO',
		 'AERO',  'JOBS',
		 'MUSEUM'
	  );
	$tld = strtoupper(substr($email, strrpos($email, '.') + 1));
	if (strlen($tld) != 2 && !in_array($tld, $TLD))
		return false;
	return true; 
}
function only_char($val){
	$length = strlen($val);
	$counter = 0;
	for ($i = 0; $i < $length; $i++) {
		$c = $val{$i};
		if ($c >= 'A' && $c <= 'Z')
			continue;
		if ($c >= 'a' && $c <= 'z')
			continue;
		if ($c == ' '){
			$counter++;
			if($counter < $length){
					continue;}
		}
		return false;
	}
	return true; 
}
function char_and_symbols($val){
	$length = strlen($val);
	$counter = 0;
	for ($i = 0; $i < $length; $i++) {
		$c = $val{$i};
		if ($c >= 'A' && $c <= 'Z')
			continue;
		if ($c >= 'a' && $c <= 'z')
			continue;
		if ($c >= '0' && $c <= '9')
            continue;
		if ($c == '.' || $c == '_' || $c == '-' || $c == '(' || $c == ')' || $c == ',')
			continue;
		if ($c == ' '){
			$counter++;
			if($counter < $length){
				continue;}
		}
		return false;
	}
	return true; 
}
function validate_email_yesSMS($email){
	if (substr_count($email, '@') != 1)
    	return false;
	if ($email{0} == '@')
		return false;
	if (substr_count($email, '.') < 1)
		return false;
	if (strpos($email, '..') !== false)
		return false;
	$length = strlen($email);
	$x=0;
	for ($i = 0; $i < $length; $i++) {
		$c = $email{$i};
		if ($c >= 'A' && $c <= 'Z')
			continue;
		if ($c >= 'a' && $c <= 'z')
			continue;
		if ($c >= '0' && $c <= '9'){
			$x++;
			if($x>=10)
				return false;
			continue;
		}
		if ($c == '@' || $c == '.' || $c == '_' || $c == '-')
			continue;
		return false;
	}
	$TLD = array (
		 'COM',   'NET',
		 'ORG',   'MIL',
		 'EDU',   'GOV',
		 'BIZ',   'NAME',
		 'MOBI',  'INFO',
		 'AERO',  'JOBS',
		 'MUSEUM'
	  );
	$tld = strtoupper(substr($email, strrpos($email, '.') + 1));
	if (strlen($tld) != 2 && !in_array($tld, $TLD))
		return false;
	return true; 
}
?>